﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using ControlAccessUser.Model;

namespace ControlAccessUser.DataAccess
{
    public class ControlAccessUserEntities : DbContext
    {
        private static ControlAccessUserEntities instance;

        public static ControlAccessUserEntities Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ControlAccessUserEntities();
                }
                return instance;
            }
        }

        public override int SaveChanges()
        {
            int result = base.SaveChanges();
            instance = new ControlAccessUserEntities();
            return result;
        }
        public void DiscardChanges()
        {
            instance = new ControlAccessUserEntities();
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Login> Logins { get; set; }

        public void LimpiarDB()
        {
            while (Instance.Logins.Count() > 0)
            {
                Instance.Logins.Remove(Instance.Logins.First());
                Instance.SaveChanges();
            }
            while (Instance.Users.Count() > 0)
            {
                Instance.Users.Remove(Instance.Users.First());
                Instance.SaveChanges();
            }
        }
    }
}
